<?php
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use think\Db;

class StoreController extends AdminBaseController
{
    //销售明细报表
    //单品报表
    public function index(){
        $list = Db::name('Store')->paginate(10);
        // 获取分页显示
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign("list", $list);
        return $this->fetch();
    }
    public function add(){
        //获取督导列表
        $user_type=get_dudao_type();
       
        $dudao_list = Db::name('RoleUser')->where(array('role_id'=>$user_type))->select()->toarray();//督导列表
        $cat_list = Db::name('StoreType')->select()->toarray();
        $goods_list = Db::name('Goods')->where(array('g_status'=>1))->select()->toarray();
        $this->assign("goods_list", $goods_list);
        $this->assign("cat_list", $cat_list);
        $this->assign("dudao_list", $dudao_list);
        return $this->fetch();
    }
    
    public function addPost(){
        $res  = $this->request->param();
        //dump($res);exit;
        $storeInfo=Db::name('StoreType')->where(array('id'=>$res['cat_store']))->find();
        $uinfo=cmf_get_current_admin_id();
        //判断手机号格式是否正确   
        if(!preg_match("/^1[34578]\d{9}$/",$res['mobile'])){
            $this->error('手机号格式不正确');
            exit;
        }
        $data=array(
            'store_name'=>$res['store_name'],
            'cat_store'=>$res['cat_store'],
            'cat_ware'=>$storeInfo['cat_goods'],
            'principal'=>$res['principal'],
            'idnumber'=>$res['idnumber'],
            'franchise_fee'=>$storeInfo['franchise_fee'],
            'mobile'=>$res['mobile'],
            'phone'=>$res['phone'],
            'address'=>$res['address'],
            'operator'=>$uinfo,
            'ctime'=>time(),
            'status'=>0,
            'steer'=>$res['steer'],            
        );
        $re=Db::name('Store')->insert($data);
        if($re){
            $this->success('添加成功','Store/index');
        }else{
            $this->error('添加失败','Store/add');
        }
    }  
    
    
    public function edit(){
        $id  = $this->request->param('id');
        $info=Db::name('Store')->where(array('id'=>$id))->find();//督导列表
        $user_type=get_dudao_type();
        $dudao_list = Db::name('RoleUser')->where(array('role_id'=>$user_type))->select()->toarray();//督导列表
       // dump($dudao_list);
        $cat_list = Db::name('StoreType')->select()->toarray();
        $goods_list = Db::name('Goods')->where(array('g_status'=>1))->select()->toarray();
        $this->assign("goods_list", $goods_list);
        $this->assign("cat_list", $cat_list);
        $this->assign("dudao_list", $dudao_list);
        $this->assign("info", $info);
        return $this->fetch();
        
    } 
    public function editPost(){
        $res  = $this->request->param();
        $storeInfo=Db::name('StoreType')->where(array('id'=>$res['cat_store']))->find();
        $uinfo=cmf_get_current_admin_id();
        
        if(!preg_match("/^1[34578]\d{9}$/",$res['mobile'])){
            $this->error('手机号格式不正确');
            exit;
        }
        if(empty($res['address'])){
            $this->error('店铺地址必须填写');
            exit;
        }
        $data=array(
            'store_name'=>$res['store_name'],
            'cat_store'=>$res['cat_store'],
            'cat_ware'=>$storeInfo['cat_goods'],
            'principal'=>$res['principal'],
            'idnumber'=>$res['idnumber'],
            'franchise_fee'=>$storeInfo['franchise_fee'],
            'mobile'=>$res['mobile'],
            'phone'=>$res['phone'],
            'address'=>$res['address'],
            'operator'=>$uinfo,
            'ctime'=>time(),
            'status'=>0,
            'steer'=>$res['steer'],
        );
        $re=Db::name('Store')->where(array('id'=>$res['id']))->update($data);
        if($re){
            $this->success('编辑成功','Store/index');
        }else{
            $this->error('编辑失败');
        }
    }
    function delete(){
        $id = $this->request->param("id");
        //判断这个类别下有没有店铺
        $checkM=Db::name('Member')->where(array('store_id'=>$id))->find();
        $checkU=Db::name('User')->where(array('store_id'=>$id))->find();
        if($checkM || $checkU){
            $this->error('该店铺下面有会员或店员，不能删除');
            exit;
        }
        $info = Db::name('Store')->where(['id' => $id])->find();
        if ($info){
            $status = Db::name('Store')->delete($id);
            if (!empty($status)) {
                $this->success("删除成功！", url('Store/index'));
            } else {
                $this->error("删除失败！");
            }
        }else{
            $this->error("该店铺不存在！");
        }
    }
  
}
